home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH13 / FILEIO.ASM < prev    next >
Encoding:
Assembly Source File  |  1993-08-24  |  4.0 KB  |  208 lines

  1. ; FILEIO
  2. ;
  3. ; This program copies the input file to the output file and adds line
  4. ; numbers while it is copying the file.
  5.  
  6.         include     stdlib.a
  7.         includelib    stdlib.lib
  8.  
  9.  
  10. dseg        segment    para public 'data'
  11.  
  12. ArgCnt        word    0
  13. LineNumber    word    0
  14. DOSErrorCode    word    0
  15. InFile        dword    ?        ;Ptr to Input file name.
  16. OutFile        dword    ?        ;Ptr to Output file name.
  17. InputLine    byte    1024 dup (0)    ;Input/Output data buffer.
  18. OutputFile    FileVar    {}
  19. InputFile    FileVar    {}
  20.  
  21. dseg        ends
  22.  
  23.  
  24. cseg        segment    para public 'code'
  25.         assume    cs:cseg, ds:dseg
  26.  
  27. ; ReadLn- Reads a line of text from the input file and stores the
  28. ;         data into the InputLine buffer:
  29.  
  30. ReadLn        proc
  31.         push    ds
  32.         push    es
  33.         push    di
  34.         push    si
  35.         push    ax
  36.  
  37.         mov    si, dseg
  38.         mov    ds, si
  39.         mov    si, offset InputLine
  40.         lesi    InputFile
  41.  
  42. GetLnLp:
  43.         fgetc
  44.         jc    RdLnDone        ;If some bizzarre error.
  45.         cmp    ah, 0            ;Check for EOF.
  46.         je    RdLnDone        ;Note:carry is set.
  47.         mov    ds:[si], al
  48.         inc    si
  49.         cmp    al, lf            ;At EOLN?
  50.         jne    GetLnLp
  51.         dec    si            ;Back up before LF.
  52.         cmp    byte ptr ds:[si-1], cr    ;CR before LF?
  53.         jne    RdLnDone
  54.         dec    si            ;If so, skip it too.
  55.  
  56. RdLnDone:    mov    byte ptr ds:[si], 0    ;Zero terminate.
  57.         pop    ax
  58.         pop    si
  59.         pop    di
  60.         pop    es
  61.         pop    ds
  62.         ret
  63. ReadLn        endp
  64.  
  65. ; MyOutput- Writes the single character in AL to the output file.
  66.  
  67. MyOutput    proc    far
  68.         push    es
  69.         push    di
  70.         lesi    OutputFile
  71.         fputc
  72.         pop    di
  73.         pop    es
  74.         ret
  75. MyOutput    endp
  76.  
  77.  
  78.  
  79. ; The main program which does all the work:
  80.  
  81. Main        proc
  82.         mov    ax, dseg
  83.         mov    ds, ax
  84.         mov    es, ax
  85.  
  86. ; Must call the memory manager initialization routine if you use
  87. ; any routine which calls malloc!  ARGV is a good example of a
  88. ; routine calls malloc.
  89.  
  90.         meminit
  91.  
  92. ; We expect this program to be called as follows:
  93. ;        fileio file1, file2
  94. ; anything else is an error.
  95.  
  96.         argc
  97.         cmp    cx, 2        ;Must have two parameters.
  98.         je    Got2Parms
  99. BadParms:    print
  100.         byte    "Usage: FILEIO infile, outfile",cr,lf,0
  101.         jmp    Quit
  102.  
  103. ; Okay, we've got two parameters, hopefully they're valid filenames.
  104. ; Get copies of the filenames and store away the pointers to them.
  105.  
  106. Got2Parms:    mov    ax, 1        ;Get the input filename
  107.         argv
  108.         mov    word ptr InFile, di
  109.         mov    word ptr InFile+2, es
  110.  
  111.         mov    ax, 2        ;Get the output filename
  112.         argv
  113.         mov    word ptr OutFile, di
  114.         mov    word ptr OutFile+2, es
  115.  
  116. ; Output the filenames to the standard output device
  117.  
  118.         printf
  119.         byte    "Input file: %^s\n"
  120.         byte    "Output file: %^s\n",0
  121.         dword   InFile, OutFile
  122.  
  123. ; Open the input file:
  124.  
  125.         lesi    InputFile
  126.         mov    dx, word ptr InFile+2
  127.         mov    si, word ptr InFile
  128.         mov    ax, 0
  129.         fopen
  130.         jnc    GoodOpen
  131.         mov    DOSErrorCode, ax
  132.         printf
  133.         byte    "Could not open input file, DOS: %d\n",0
  134.         dword    DOSErrorCode
  135.         jmp    Quit
  136.  
  137. ; Create a new file for output:
  138.  
  139. GoodOpen:    lesi    OutputFile
  140.         mov    dx, word ptr OutFile+2
  141.         mov    si, word ptr OutFile
  142.         fcreate
  143.         jnc    GoodCreate
  144.         mov    DOSErrorCode, AX
  145.         printf
  146.         byte    "Could not open output file, DOS: %d\n",0
  147.         dword    DOSErrorCode
  148.         jmp    Quit
  149.  
  150. ; Okay, save the output hook and redirect the output.
  151.  
  152. GoodCreate:    PushOutAdrs
  153.         lesi    MyOutput
  154.         SetOutAdrs
  155.  
  156.  
  157. WhlNotEOF:    inc    LineNumber
  158.  
  159. ; Okay, read the input line from the user:
  160.  
  161.         call    ReadLn
  162.         jc    BadInput
  163.  
  164. ; Okay, redirect the output to our output file and write the last line
  165. ; read prefixed with a line number:
  166.  
  167.         printf
  168.         byte    "%4d:   %s\n",0
  169.         dword   LineNumber, InputLine
  170.         jmp    WhlNotEOF
  171.  
  172.  
  173. BadInput:    push    ax        ;Save error code.
  174.         PopOutAdrs        ;Restore output hook.
  175.         pop    ax        ;Retrieve error code.
  176.         test    ax, ax        ;EOF error? (AX = 0)
  177.         jz    CloseFiles
  178.         mov     DOSErrorCode, ax
  179.         printf
  180.         byte    "Input error, DOS: %d\n",0
  181.         dword    LineNumber
  182.  
  183. ; Okay, close the files and quit:
  184.  
  185. CloseFiles:     lesi    OutputFile
  186.         fclose
  187.         lesi    InputFile
  188.         fclose
  189.  
  190. Quit:        ExitPgm            ;DOS macro to quit program.
  191. Main        endp
  192.  
  193. cseg            ends
  194.  
  195.  
  196.  
  197. sseg        segment    para stack 'stack'
  198. stk        db    1024 dup ("stack   ")
  199. sseg        ends
  200.  
  201.  
  202. ;zzzzzzseg is required by the standard library routines.
  203.  
  204. zzzzzzseg    segment    para public 'zzzzzz'
  205. LastBytes    db    16 dup (?)
  206. zzzzzzseg    ends
  207.         end    Main
  208.